// 全选和非全选功能
let allBtn = document.querySelector('.goods .goodslist .allBtn input')
let checkboxs = document.querySelectorAll('.goods .list input')
let footerAllBtn = document.querySelector('.list-total .allBtn')
let amounts = document.querySelectorAll('.list .amount')
let rows = document.querySelectorAll('.list ul')

// 全选功能
SelectAll(allBtn,footerAllBtn)
SelectAll(footerAllBtn,allBtn)

// 如果所有列表都勾选，全选按钮自动选上
for(let i=0;i<checkboxs.length;i++){
    checkboxs[i].addEventListener('click',function(){
        let num = document.querySelectorAll('.goods .list input:checked')
        if(num.length === checkboxs.length){
            allBtn.checked = footerAllBtn.checked = true
        }else{
            allBtn.checked = footerAllBtn.checked = false
        }
    })
}

// 全选函数
function SelectAll(Event_element,Response_element){
    Event_element.addEventListener('click',function(){
        for(let i=0;i<checkboxs.length;i++){
            checkboxs[i].checked=this.checked
            Response_element.checked = Event_element.checked
        }
    })
}

// 减数量
let reduce = document.querySelectorAll('.reduce') 
for(let i = 0;i<reduce.length;i++){
    reduce[i].addEventListener('click',function(){
        let n = +this.nextElementSibling.innerText
        if(n>1){
            n-=1
        }
        this.nextElementSibling.innerText = n
        addAamout()
        total()
    })
}

// 加数量
let plus = document.querySelectorAll('.plus')
for(let i = 0;i<plus.length;i++){
    plus[i].addEventListener('click',function(){
        let n = +this.previousElementSibling.innerText
        n+=1
        this.previousElementSibling.innerText = n
        addAamout()
        total()
    })
}

// 结算单个商品金额
function addAamout(){
    
    // 遍历每个商品行并计算金额
    for (let i = 0; i < rows.length; i++) {
        // 获取单价和数量
        let price = parseInt(rows[i].querySelector('.price').innerHTML) || 0;
        let pnum = parseInt(rows[i].querySelector('.pnum span').innerHTML) || 0;
    
        // 计算金额并更新到页面
        amounts[i].innerHTML = price * pnum;
    }
}
addAamout()

//修改商品数量总计
let right_info_num = document.querySelector('.right-info-num span')
right_info_num.innerHTML = rows.length

// 修改商品金额总计
function total(){
    let right_info_total = document.querySelector('.right-info-total span')
    let total = 0
    for(let i=0;i<amounts.length;i++){
        total+=parseFloat(amounts[i].innerHTML)
    }
    right_info_total.innerHTML = total.toFixed(2)
}
total()

// 做了第一次修改

// 做了第二次修改




















